# -*- coding: utf-8 -*-
# @Time : 2024/3/17 10:56 AM 
# @Author : Wayne
# @Email : 1
# @File : data_change.py
# @Project : young_python_demo
# @DESCRIPTION :

"""
将无意义数组转换为固定 key 的字典数组
多用于金蝶
固定数据返回是数组，没有 key，有时候无法区分数据的字段，开发或者查询数据有困难
"""
def convert_to_objects(data, keys):
    obj_list = []
    for item in data:
        obj = dict(zip(keys, item))
        obj_list.append(obj)
    return obj_list

"""
将传入的字符 key 做数组转换
"""
def process_field_keys(FieldKeys):
    """
    入参:金蝶的FieldKeys,示例FNumber,FName,FMaterialGroup.FName,FBaseUnitId.FName,FDescription
    批量给FieldKeys的每个字段加上双引号
    出参:['FNumber', 'FName', 'FMaterialGroup_FName', 'FBaseUnitId_FName', 'FDescription']
    """
    # 使用split函数将字符串按逗号分割成列表
    keys_list = FieldKeys.split(',')
    processed_keys = []
    for key in keys_list:
        # 使用replace函数将"."替换为"_"
        processed_key = key.replace(".", "_")

        # 将处理后的键添加到结果列表
        processed_keys.append(processed_key)
    return processed_keys

data = [
  [
    "DBSQ002174",
    "胡超鸿",
    129139,
    "BC300000",
    "Paulmann 德国柏曼BC300000(3C型号BC300000)Achim柜灯1W雅光银",
    20,
    "CP001",
    "CP002",
    "个"
  ],
  [
    "DBSQ002174",
    "胡超鸿",
    129140,
    "BC300001",
    "Paulmann 德国柏曼BC300001(3C型号BC300001)Achten柜灯1W雅光银",
    20,
    "CP001",
    "CP002",
    "个"
  ]
]
FieldKeys = "FBillNo,FCreatorId.fname,FEntity_FEntryID,FMATERIALID.fnumber, FMaterialName,FBASEQTY,FStockId.fnumber,FStockInId.fnumber,FUNITID.fname"
keys = process_field_keys(FieldKeys)
print(convert_to_objects(data,keys))